package com.qqtech.qquan.pub.service;

import java.util.List;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.stereotype.Service;

import com.qqtech.core.common.enums.BizzLevel2Enum;
import com.qqtech.core.frame.dao.BaseDao;
import com.qqtech.core.frame.model.PageKit;
import com.qqtech.core.frame.model.PageOrder;
import com.qqtech.core.frame.service.BaseServiceImpl;
import com.qqtech.qquan.circle.dao.CircleTopicDao;
import com.qqtech.qquan.circle.model.CircleTopic;
import com.qqtech.qquan.community.dao.CommunityMessageDao;
import com.qqtech.qquan.community.model.CommunityMessage;
import com.qqtech.qquan.pub.dao.PubPraiseDao;
import com.qqtech.qquan.pub.enums.PubPraiseStatusDictEnum;
import com.qqtech.qquan.pub.model.PubPraise;
import com.qqtech.qquan.pub.model.PubPraiseQuery;
import com.qqtech.qquan.pub.model.PubPraiseVo;
import com.qqtech.qquan.user.dao.UserTopicDao;
import com.qqtech.qquan.user.model.UserTopic;

@Service
public class PubPraiseServiceImpl extends BaseServiceImpl<PubPraise>
		implements PubPraiseService {
	public static final Logger log = LoggerFactory.getLogger(PubPraiseServiceImpl.class);
	
	@Resource
	private PubPraiseDao pubPraiseDao;
	@Resource
	private CircleTopicDao CircleTopicDao;
	@Resource
	private CommunityMessageDao communityMessageDao;
	@Resource
	private UserTopicDao userTopicDao;

	@Override
	protected BaseDao<PubPraise> getBaseDao() {
		return pubPraiseDao;
	}

	@Override
	public int getPraiseCountByBizz(int bizzId, String bizzType) {
		PubPraiseQuery praise = new PubPraiseQuery();
		praise.setBizzId(bizzId);
		praise.setBizzType(bizzType);
		praise.setStatus(PubPraiseStatusDictEnum.NORMAL.getCode());
		int count = pubPraiseDao.queryCount(praise);
		return count;
	}

	@Override
	public List<PubPraise> queryPraiseListByBizz(int bizzId, String bizzType) {
		PubPraiseQuery praise = new PubPraiseQuery();
		praise.setBizzId(bizzId);
		praise.setBizzType(bizzType);
		praise.setStatus(PubPraiseStatusDictEnum.NORMAL.getCode());
		Pageable pageKit = new PageKit(praise.getPage(), new PageOrder(Direction.DESC, "id"));
		List<PubPraise> list  = pubPraiseDao.queryList(praise, pageKit);
		return list;
	}

	@Override
	public Integer getBeUserId(int bizzId, String bizzType) {
		if(BizzLevel2Enum.CIRCLE_TOPIC.getCode().equals(bizzType)){
			CircleTopic circleTopic = CircleTopicDao.getById(bizzId);
			if(circleTopic != null){
				return circleTopic.getCreatorId();
			}
		}else if(BizzLevel2Enum.COMMUNITY_MESSAGE.getCode().equals(bizzType)){
			CommunityMessage communityMessage = communityMessageDao.getById(bizzId);
			if(communityMessage != null){
				return communityMessage.getCreatorId();
			}
		}else if(BizzLevel2Enum.USER_TOPIC.getCode().equals(bizzType)){
			UserTopic userTopic = userTopicDao.getById(bizzId);
			if(userTopic != null){
				return userTopic.getCreatorId();
			}
		}
		return null;
	}

	@Override
	public boolean isPraisedByUser(int bizzId, String bizzType, Integer userId) {
		PubPraiseQuery praise = new PubPraiseQuery();
		praise.setBizzId(bizzId);
		praise.setBizzType(bizzType);
		praise.setCreatorId(userId);
		praise.setStatus(PubPraiseStatusDictEnum.NORMAL.getCode());
		return pubPraiseDao.queryCount(praise) > 0;
	}

	@Override
	public boolean isFirstPraiseDynamic(Integer userId, BizzLevel2Enum bizzLevel2) {
		PubPraiseQuery praiseQ = new PubPraiseQuery();
		praiseQ.setCreatorId(userId);
		praiseQ.setBizzType(bizzLevel2.getCode());
		int count = pubPraiseDao.queryCount(praiseQ);
		return (count)==0;
	}
	
	@Override
	public List<PubPraiseVo> myLastSomePraises(Integer userId, Integer some, BizzLevel2Enum bizzLevel2) {
		PubPraiseQuery prasieQ = new PubPraiseQuery();
		prasieQ.setCreatorId(userId);
		if(bizzLevel2!=null){
			prasieQ.setBizzType(bizzLevel2.getCode());
		}
		prasieQ.setStatus(PubPraiseStatusDictEnum.NORMAL.getCode());
		Pageable pageKit = new PageKit(1,some, new PageOrder(Direction.DESC, "newAt"));
		return pubPraiseDao.queryList(prasieQ, pageKit);
	}
}